﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public static class Extension
{
    public static bool isNull(object value, Type type)
    {
        if (value == null) return true;

        //if (type == typeof(int) && value.ToInt() == default(int))
        //{
        //    return true;
        //}
        if (type == typeof(double) && value.ToDouble() == default(double))
        {
            return true;
        }
        if (type == typeof(DateTime) && value.ToDateTime() == default(DateTime))
        {
            return true;
        }
        if (type == typeof(float) && value.ToFloat() == default(float))
        {
            return true;
        }
        if (type == typeof(long) && value.ToLong() == default(long))
        {
            return true;
        }
        if (type == typeof(decimal) && value.ToDecimal() == default(decimal))
        {
            return true;
        }
        if (type == typeof(short) && value.ToShort() == default(short))
        {
            return true;
        }
        return false;
    }
    public static Int16 ToShort(this object obj)
    {
        return Convert.ToInt16(obj);
    }
    public static Int32 ToInt(this object obj)
    {
        return Convert.ToInt32(obj);
    }
    public static Int64 ToLong(this object obj)
    {
        return Convert.ToInt64(obj);
    }
    public static decimal ToDecimal(this object obj)
    {
        return Convert.ToDecimal(obj);
    }
    public static float ToFloat(this object obj)
    {
        return Convert.ToSingle(obj);
    }
    public static double ToDouble(this object obj)
    {
        return Convert.ToDouble(obj);
    }
    public static DateTime ToDateTime(this object obj)
    {
        return Convert.ToDateTime(obj);
    }

    public static int ToInt(this string str)
    {
        return Convert.ToInt32(str);
    }

    public static int? ToIntOrDefault(this string str)
    {
        int result;
        if (int.TryParse(str, out result))
        {
            return result;
        }
        return null;
    }

    public static long? ToLongOrDefault(this string str)
    {
        long result;
        if (long.TryParse(str, out result))
        {
            return result;
        }
        return null;
    }

    /// <summary>
    /// 打乱集合顺序
    /// </summary>
    /// <typeparam name="T">集合类型</typeparam>
    /// <param name="list">集合</param>
    /// <returns></returns>
    public static IList<T> Shuffle<T>(this IList<T> list)
    {
        Random random = new Random();
        for (int i = 0; i < list.Count; i++)
        {
            int index = random.Next(0, list.Count);
            T item = list[index];
            list[index] = list[i];
            list[i] = item;
        }
        return list;
    }
}

